#include "image_filter.h"

#include "base/logging.h"
#include "base/config_reader.h"

namespace im {

bool ImageFilter::IsLegal(int64_t qid) {
  boost::unique_lock<boost::shared_mutex> wlock(mutex_);
  std::map<int64_t, std::vector<time_t> >::iterator it = filter_map_.find(qid); 

  time_t t = time(NULL);

  if (it != filter_map_.end()) {
    if (it->second.size() < 8) {
      it->second.push_back(t);
      LOG_DEBUG("qid " << qid << " image OK 2.");
      return true;
    }

    if (t - it->second.back() < 5) {
      LOG_INFO("qid " << qid << " image dropped[interval < 5s]");
      it->second.push_back(t);
      return false;
    }
    it->second.push_back(t);
    // 第一张图以来发送的图片总数
    time_t sec_since_first = t - it->second.front();
    if (sec_since_first / it->second.size() < 600) {
      LOG_INFO("qid " << qid << " image dropped[sec_since_first=" << sec_since_first << " total_sent=" << it->second.size() << "]");
      return false;
    }
    LOG_DEBUG("qid " << qid << " image OK 3.");
    return true;
  } else {
    LOG_DEBUG("qid " << qid << " image OK 1.");
    filter_map_[qid].push_back(t);
    return true;
  }
}


}

